/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.grafico;

import br.ufc.quixada.adrs.model.RelatorioQualitativoResult;
import br.ufc.quixada.adrs.util.Msg;
import java.awt.Color;
import java.awt.Paint;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.TextAnchor;

/**
 *
 * @author Escritorio projetos
 */
public class ADRSStackedBarChart {

    private String title = "";

    public ADRSStackedBarChart(String title) {
        this.title = title;
    }

    public JFreeChart criarChart(final CategoryDataset dataset, String tipoGrafico, PlotOrientation plotOrientation) {


        final JFreeChart chart = ChartFactory.createStackedBarChart3D(
                title, "", "",
                dataset, plotOrientation, true, true, true);

        chart.setBackgroundPaint(new Color(255, 255, 255));

        CategoryPlot plot = chart.getCategoryPlot();

        //Exibe numeros na opcoes no stackd bar chart
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setBaseItemLabelGenerator(
                new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);

        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.CENTER, TextAnchor.CENTER));
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.CENTER, TextAnchor.CENTER));
      
//        ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT);
//        renderer.setPositiveItemLabelPositionFallback(position);

        //Gera grafico com porcentagem
//        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//        rangeAxis.setNumberFormatOverride(new DecimalFormat("0%"));//Mostra eixo y com valores de 0 a 100% a exclusão dessas 2 linhas deixa os numeros no intervalo de 0.0 a 0.1
//
//        StackedBarRenderer3D renderer =
//                (StackedBarRenderer3D) plot.getRenderer();
//        renderer.setRenderAsPercentages(true);
//        renderer.setDrawBarOutline(false);
//        renderer.setBaseItemLabelGenerator(
//                new StandardCategoryItemLabelGenerator("{3}",
//                NumberFormat.getIntegerInstance(),
//                new DecimalFormat("0.0%")));
//        renderer.setBaseItemLabelsVisible(true);
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.CENTER, TextAnchor.CENTER));
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.CENTER, TextAnchor.CENTER));
//        renderer.setBaseItemLabelFont(new Font("Arial", Font.PLAIN, 10));
        renderer.setMinimumBarLength(0.10);
        renderer.setMaximumBarWidth(0.10);

        CategoryAxis categoryAxis = plot.getDomainAxis();
        categoryAxis.setMaximumCategoryLabelLines(100);
        dataset.getRowKeys().get(0);
        Paint p = (Paint) renderer.getSeriesOutlinePaint(0);

        if (tipoGrafico.equals(Msg.GRAPHIC_QL_AVALIACAO)) {
            //Altera cores das colunas de acordo com a serie
            List seriesName = dataset.getRowKeys();
            for (int i = 0; i < seriesName.size(); i++) {
                String serieName = (String) seriesName.get(i);
                if (serieName.equalsIgnoreCase(RelatorioQualitativoResult.RESPOSTA_SIM)) {
                    renderer.setSeriesPaint(i, new Color(30, 100, 175));//Azul
//                    renderer.setSeriesPaint(i, Color.blue);//Azul
                } else if (serieName.equalsIgnoreCase(RelatorioQualitativoResult.RESPOSTA_SIM_ADEQUADAMENTE)) {
                    renderer.setSeriesPaint(i, new Color(90, 190, 110));//Verde
//                    renderer.setSeriesPaint(i, Color.green);//Verde
                } else if (serieName.equalsIgnoreCase(RelatorioQualitativoResult.RESPOSTA_NAO)) {
                    renderer.setSeriesPaint(i, new Color(225, 45, 45));//Vermelho
//                    renderer.setSeriesPaint(i, Color.red);
                } else if (serieName.equalsIgnoreCase(RelatorioQualitativoResult.RESPOSTA_NAO_RESPONDIDO)) {
                    renderer.setSeriesPaint(i, Color.GRAY);
                }
            }
        }
        renderer.setBaseSeriesVisible(true);
        return chart;
    }
}
